
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
jQuery.noConflict();
var aeRequest;
var action;
var ajxUrl= "";
var userid;
var username;
var password;
var type;
var shiptype;
var contactperson;
var designation;
var city;
var address;
var email;
var telephone;
var mobile;
var faxnumber;
var description;
var experience;
var userstatus;
var IdArr = new Array();
var compound = /^[0-9\s]+$/gi;
var rowId;
var flag;
var rowCount;

function createXmlHttpRequestObject() {
    var ro;
    var browser = navigator.appName;
    // Need to determine IE7 and not do this.
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function createResponseXML(textXML) {
    // code for IE
    if (window.ActiveXObject) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(textXML);
        return xmlDoc;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(textXML, "text/xml");
        return xmlDoc;
    }
}

function updateUser(userid,type,paymenttype,password,username,shiptype,contactperson,designation,address,city,mobile,telephone,faxnumber,email,experience,description,userstatus,action){
    var url = '/RadioCabsProject-war/UserRadioCabServlet?userid=' +userid+ '&type=' +type+
                '&paymenttype=' +paymenttype+ '&username=' +username+
                '&password=' +password+ '&shiptype=' +shiptype+ '&contactperson=' +contactperson+
                '&designation='  +designation+ '&address=' +address+ '&city=' +city+
                '&mobile=' +mobile+ '&telephone=' +telephone+ '&faxnumber=' +faxnumber+
                '&email=' +email+ '&experience=' +experience+ '&description=' +description+
                '&userstatus=' +userstatus+ '&action=' + action;
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function randomString() {
	var chars = "0123456789";
	var string_length = 6;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
	var rnum = Math.floor(Math.random() * chars.length);
	randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}
String.prototype.trim = function()
{
	var trimmed = this.replace(/^\s+|\s+$/g, '') ;
	return trimmed;
};

function logoutAdmin(){
    var url = '/RadioCabsProject-war/LogoutServlet';
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishLogout;
    aeRequest.send(null);
}

function finishLogout(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            window.location.href="../Home/home.jsp";
        }
    }
}

function finishUpdate(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            var response = createResponseXML(aeRequest.responseText);
            var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;
            if (result == "success") {
                alert("update/edit successful");
            }else{
                alert("update/edit failed");
            }
        }
    }
}

function setEditFormPosition(ele){
    var pos = jQuery("#"+ele).position();
    jQuery("#editContainer").css({
        position : "absolute",
        top : pos.top + 18 + "px",
        left : pos.left - 885 + "px"
    });
}

function setDeleteFormPosition(){
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = jQuery("#deleteContainer").height();
    var popupWidth = jQuery("#deleteContainer").width();

    jQuery("#deleteContainer").css({
        "position" : "absolute",
        "top" : (windowHeight/2 - popupHeight/2)-50,
        "left" : (windowWidth/2 - popupWidth/2)+150
    });
}
function randomString() {
    var chars = "0123456789";
    var string_length = 6;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    return randomstring;
}
String.prototype.trim = function()
{
    var trimmed = this.replace(/^\s+|\s+$/g, '') ;
    return trimmed;
};
jQuery(document).ready(function() {
    jQuery(".editForm").live('click',function(){
        flag = "edit";
        id = this.id;
        rowId = jQuery(this).parent().parent().attr('id');

        userid = jQuery(this).parent().parent().find('td:eq(0)').html();
        username = jQuery(this).parent().parent().find('td:eq(1)').html();
        password = jQuery(this).parent().parent().find('td:eq(2)').html();
        contactperson = jQuery(this).parent().parent().find('td:eq(3)').html();
        designation = jQuery(this).parent().parent().find('td:eq(4)').html();
        address = jQuery(this).parent().parent().find('td:eq(5)').html();
        city = jQuery(this).parent().parent().find('td:eq(6)').html();
        email = jQuery(this).parent().parent().find('td:eq(7)').html();
        mobile = jQuery(this).parent().parent().find('td:eq(8)').html();
        telephone = jQuery(this).parent().parent().find('td:eq(9)').html();
        faxnumber = jQuery(this).parent().parent().find('td:eq(10)').html();
        experience = jQuery(this).parent().parent().find('td:eq(11)').html();
        description = jQuery(this).parent().parent().find('td:eq(12)').html();
        usertype = jQuery(this).parent().parent().find('td:eq(13)').html();
        userstatus = jQuery(this).parent().parent().find('td:eq(14)').html();
        shiptype = jQuery(this).parent().parent().find('td:eq(15)').html();
        paymenttype = jQuery(this).parent().parent().find('td:eq(16)').html();

        if(userstatus == "Paid"){
            userstatus = true;
        }else{
            userstatus = false;
        }
        if(usertype == "Listing"){
            usertype = 7;
        }else if(usertype == "Driver"){
            usertype = 8;
        }else if(usertype == "Advertise"){
            usertype = 9;
        }
        if(experience == "null"){
            experience="";
        }
        if(description == "null"){
            description="";
        }
        if(usertype == "null"){
            usertype="";
        }
        if(shiptype == "null"){
            shiptype="";
        }
        if(paymenttype == "null"){
            paymenttype="";
        }
        if(jQuery("#editContainer").is(":hidden")){
            setEditFormPosition(id);
            jQuery("#editContainer").slideDown("slow");

            jQuery("#txtUser").val(username);
            jQuery("#txtPass").val(password);
            jQuery("#txtAdd").val(address);
            jQuery("#txtEmail").val(email);
            jQuery("#txtPhone").val(telephone);
            jQuery("#txtCity").val(city);
        }else{
            jQuery("#editContainer").slideUp("slow");
        }
    });
    jQuery("#editCloseForm").click(function(){
        jQuery("#editContainer").slideUp("slow");
    });

    jQuery("#saveEdit").live('click',function(){
        rowCount = jQuery("#tblUser tr").size();
        if(flag == "edit"){
            username = jQuery("#txtUser").val();
            password = jQuery("#txtPass").val();
            address = jQuery("#txtAdd").val();
            email = jQuery("#txtEmail").val();
            telephone = jQuery("#txtPhone").val();
            city = jQuery("#txtCity").val();

            if(username!=null && username.length>0 && password!=null && password.length>0 && String(telephone).match(compound) && String(faxnumber).match(compound)){
                if(usertype== 7){
                    updateUser(userid, usertype, paymenttype, password, username, shiptype, contactperson, designation, address, city, mobile, telephone, faxnumber, email, experience, description, userstatus, "edit");
                    jQuery("#" + rowId).find('td:eq(1)').html(username);
                    jQuery("#" + rowId).find('td:eq(2)').html(password);
                    jQuery("#" + rowId).find('td:eq(5)').html(address);
                    jQuery("#" + rowId).find('td:eq(7)').html(email);
                    jQuery("#" + rowId).find('td:eq(9)').html(telephone);
                    jQuery("#" + rowId).find('td:eq(10)').html(faxnumber);
                    jQuery("#editContainer").slideUp("slow");
                }else if(usertype == 8){
                    updateUser(userid, usertype, paymenttype, password, username, shiptype, contactperson, designation, address, city, mobile, telephone, faxnumber, email, experience, description, userstatus, "edit");
                    jQuery("#" + rowId).find('td:eq(1)').html(username);
                    jQuery("#" + rowId).find('td:eq(2)').html(password);
                    jQuery("#" + rowId).find('td:eq(5)').html(address);
                    jQuery("#" + rowId).find('td:eq(7)').html(email);
                    jQuery("#" + rowId).find('td:eq(9)').html(telephone);
                    jQuery("#" + rowId).find('td:eq(10)').html(faxnumber);
                    jQuery("#editContainer").slideUp("slow");
                }else if(usertype == 9){
                    updateUser(userid, usertype, paymenttype, password, username, shiptype, contactperson, designation, address, city, mobile, telephone, faxnumber, email, experience, description, userstatus, "edit");
                    jQuery("#" + rowId).find('td:eq(1)').html(username);
                    jQuery("#" + rowId).find('td:eq(2)').html("");
                    jQuery("#" + rowId).find('td:eq(5)').html(address);
                    jQuery("#" + rowId).find('td:eq(7)').html(email);
                    jQuery("#" + rowId).find('td:eq(9)').html(telephone);
                    jQuery("#" + rowId).find('td:eq(10)').html(faxnumber);
                    jQuery("#editContainer").slideUp("slow");
                }

            }else{
                alert("Username, Password can not be empty. Phone,Fax must be number");
            }
        }
    });

    jQuery(".delForm").live('click',function(){
        id = this.id;
        rowId = jQuery(this).parent().parent().attr('id');

        userid = jQuery(this).parent().parent().find('td:eq(0)').html();
        username = jQuery(this).parent().parent().find('td:eq(1)').html();
        password = jQuery(this).parent().parent().find('td:eq(2)').html();
        contactperson = jQuery(this).parent().parent().find('td:eq(3)').html();
        designation = jQuery(this).parent().parent().find('td:eq(4)').html();
        address = jQuery(this).parent().parent().find('td:eq(5)').html();
        city = jQuery(this).parent().parent().find('td:eq(6)').html();
        email = jQuery(this).parent().parent().find('td:eq(7)').html();
        mobile = jQuery(this).parent().parent().find('td:eq(8)').html();
        telephone = jQuery(this).parent().parent().find('td:eq(9)').html();
        faxnumber = jQuery(this).parent().parent().find('td:eq(10)').html();
        experience = jQuery(this).parent().parent().find('td:eq(11)').html();
        description = jQuery(this).parent().parent().find('td:eq(12)').html();
        usertype = jQuery(this).parent().parent().find('td:eq(13)').html();
        userstatus = jQuery(this).parent().parent().find('td:eq(14)').html();
        shiptype = jQuery(this).parent().parent().find('td:eq(15)').html();
        paymenttype = jQuery(this).parent().parent().find('td:eq(16)').html();

        if(userstatus == "Paid"){
            userstatus = true;
        }else{
            userstatus = false;
        }
        if(usertype == "Listing"){
            usertype = 7;
        }else if(usertype == "Driver"){
            usertype = 8;
        }else if(usertype == "Advertise"){
            usertype = 9;
        }
        if(experience == "null"){
            experience="";
        }
        if(description == "null"){
            description="";
        }
        if(usertype == "null"){
            usertype="";
        }
        if(shiptype == "null"){
            shiptype="";
        }
        if(paymenttype == "null"){
            paymenttype="";
        }

        if(jQuery("#deleteContainer").is(":hidden")){
            setDeleteFormPosition();
            jQuery("#deleteContainer").slideDown("slow");
        }else{
            jQuery("#deleteContainer").slideUp("slow");
        }
    });

    jQuery("#noDel").live('click',function(){
        jQuery("#deleteContainer").hide("explode",1000);
    });

    jQuery("#yesDel").live('click',function(){
        id = this.id;
        updateUser(userid, usertype, paymenttype, password, username, shiptype, contactperson, designation, address, city, mobile, telephone, faxnumber, email, experience, description, userstatus, "delete");

        jQuery("#"+rowId).remove();

        jQuery("#deleteContainer").hide("explode",1000);
    });

    jQuery("#btnOut").live('click',function(){
        logoutAdmin();
    });
});


